package logic

import (
	"context"
	"fmt"
	"taobao_svc/models/mysql"

	"taobao_svc/internal/svc"
	"taobao_svc/taobao"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 购物车添加
func (l *ProductAddLogic) ProductAdd(in *taobao.ProductAddRequest) (*taobao.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Name:        in.Name,
		CategoryID:  uint(in.CategoryId),
		Description: in.Description,
		Price:       float64(in.Price),
		Stock:       int(in.Stock),
		Status:      int8(in.Status),
	}

	mysql.DB.Debug().Create(&product)

	if product.Stock < int(in.Stock) {
		return nil, fmt.Errorf("商品库存不足")
	}

	return &taobao.ProductAddResponse{
		Id: int64(product.ID),
	}, nil
}
